<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Member_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

	function get_address_by_uid($user_id){
		// return $this->db->select('user_dress.*,areas.area_name')->from('user_dress')->join('areas','areas.area_id=user_dress.dress_province')->get()->result_array();
		$query = $this->db->select('*')->from('user_dress')->where('user_id',$user_id)->where('delete',0)->order_by('dress_type','desc')->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
        // return $query->result_array();
	}
	function get_adress_by_user_id($user_id){
		$query = $this->db->select('*')->from('user_dress')->where(array('user_id'=>$user_id,'dress_type'=>1))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
        // return $query->row_array();
	}
	function add_dress($data){
		$this->db->insert('user_dress',$data);
		return $this->db->insert_id();
//        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function get_collection_by_user_id($user_id){
		$query = $this->db->select('*')->from('user_favorites')->where('user_id',$user_id)->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_item_name_by_id($item_id=0,$item_type=0){
		if($item_type == 0){
			$query = $this->db->select('*')->from('shop_goods')->where('good_id',$item_id)->get();
			if($query->num_rows() > 0){
            return $query->row_array();
        	}
			// return $query->row_array();
		}elseif($item_type == 2){
			$query = $this->db->select('*')->from('articles')->where('article_id',$item_id)->get();
			if($query->num_rows() > 0){
            return $query->row_array();
        	}
			// return $query->row_array();
		}else{
			$query = $this->db->select('*')->from('shop_users')->where('shop_id',$item_id)->get();
			if($query->num_rows() > 0){
            return $query->row_array();
        	}
			// return $query->row_array();
		}
		
	}
	function del_favorite($good_id,$user_id){
		$this->db->where('item_id',$good_id)->where('user_id',$user_id)->where('item_type',0)->delete('user_favorites');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	//从列表删除收藏单品
	public function del_favorites($favorite_id,$user_id){
		$this->db->where('favorite_id',$favorite_id)->where('user_id',$user_id)->delete('user_favorites');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function get_dress_by_user_id($user_id){
		$query = $this->db->select('*')->from('user_dress')->where(array('user_id' => $user_id, 'dress_type'=>1,'delete'=>0))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function change_dress_by_user_id($user_id,$data){
		$this->db->where(array('user_id'=>$user_id,'dress_type'=>1));
        $this->db->update('user_dress', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function change_dress_type($dress_id,$data){
		$this->db->where(array('dress_id'=>$dress_id));
        $this->db->update('user_dress', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function get_footprint_by_user_id($user_id){
		$query = $this->db->select('*')->from('user_views')->where('user_id',$user_id)->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_goods_by_goods_id($good_id){
		$query = $this->db->select('*')->from('shop_goods')->where(array('good_id' => $good_id))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function add_favorites($data){
		$this->db->insert('user_favorites',$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function get_footpoint_by_id($user_id,$good_id){
		$result = $this->db->select('*')->from('user_views')->where(array('user_id'=>$user_id,'item_id'=>$good_id,'item_type'=>0))->get();
	}
	function add_goods_footpoint($data){		
			$this->db->insert('user_views',$data);
        	return ($this->db->affected_rows() > 0) ? TRUE : FALSE;		
	}
	function is_favorites($user_id,$item_id){
		$query = $this->db->select('*')->from('user_favorites')->where(array('user_id' => $user_id, 'item_id'=>$item_id))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function is_footprint($user_id,$item_id){
		$query = $this->db->select('*')->from('user_views')->where(array('user_id' => $user_id, 'item_id'=>$item_id))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function save_goods_footpoint($view_id,$arr){
		$this->db->where('view_id',$view_id);
        $this->db->update('user_views', $arr);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	function updates_goods_footpoint($id){
		$sql = "update {$this->db->dbprefix}user_views set view_num=view_num +1 where view_id = ".$id;
        $query=$this->db->query($sql);
	}
	function get_integral_by_user_id($user_id){
		$query = $this->db->select('*')->from('credits')->where('user_id',$user_id)->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_money_by_user_id($user_id){
		$query = $this->db->select('*')->from('moneys')->where('user_id',$user_id)->order_by('money_time','desc')->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_shop_orders_by_user_id($user_id){
		$query = $this->db->select('*')->from('shop_orders')->where(array('user_id'=>$user_id,'order_status'=>2))->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_all_orders_by_user_id($user_id){
		$query = $this->db->select('*')->from('shop_orders')->where('user_id',$user_id)->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_goods_name_by_id($id){
		$query = $this->db->select('*')->from('shop_goods')->where('good_id',$id)->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function get_get_deliver_by_id($id){
		$query = $this->db->select('*')->from('DeliverApi')->where('order_id',$id)->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function get_order_by_order_id($id){
		$query = $this->db->select('*')->from('shop_orders')->where('order_id',$id)->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}
	function get_frees_by_user_id($user_id){
		$query = $this->db->select('*')->from('free_vouchers')->where(array('user_id'=>$user_id,'voucher_lock'=>0))->get();
		if($query->num_rows() > 0){
            return $query->result_array();
        }
		// return $query->result_array();
	}
	function get_frees_count_by_user_id($user_id){
		$this->db->select('voucher_id');
		$this->db->from('free_vouchers');
		$this->db->where('user_id',$user_id);
        $this->db->where('voucher_lock',0); // 可用的
        $this->db->where('voucher_end_time>',time());
		$total=$this->db->count_all_results();
		return $total;
	}
	public function get_favorite($user_id,$good_id){
		$query = $this->db->select('*')->from('user_favorites')->where(array('item_type'=>0,'user_id'=>$user_id,'item_id'=>$good_id))->get();
		if($query->num_rows() > 0){
            return $query->row_array();
        }
		// return $query->row_array();
	}

	public function month_user_credit($user_id)
	{
		$bg_time = strtotime(date("Y-m-01"));
		$bg_time_link = date('Y-m-01', strtotime(date("Y-m-d")));
		$end_time = strtotime("$bg_time_link +1 month -1 day");
		$this->db->select("sum(credit_num) as credit_num");//sum(money_num)
		$this->db->from('credits');
		$this->db->where('user_id',$user_id);
		$this->db->where('credit_time>',$bg_time);
		$this->db->where('credit_time<',$end_time);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
	}

	public function user_chongzhi($user_id)
	{
		$bg_time = strtotime(date("Y-m-01"));
		$bg_time_link = date('Y-m-01', strtotime(date("Y-m-d")));
		$end_time = strtotime("$bg_time_link +1 month -1 day");
		$this->db->select("sum(money_num) as chongzhi");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',3);
		$this->db->where('money_time>',$bg_time);
		$this->db->where('money_time<',$end_time);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
	}
	public function user_zhichu($user_id)
	{
		$bg_time = strtotime(date("Y-m-01"));
		$bg_time_link = date('Y-m-01', strtotime(date("Y-m-d")));
		$end_time = strtotime("$bg_time_link +1 month -1 day");
		$this->db->select("sum(money_num) as zhichu");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',2);
		$this->db->where('money_time>',$bg_time);
		$this->db->where('money_time<',$end_time);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
	}
	public function user_zhichu_month($user_id,$month)
	{
		$bg_time = strtotime(date("Y-".$month."-01"));
		$bg_time_link = date('Y-m-01', strtotime(date("Y-m-d")));
		$end_time = strtotime("$bg_time_link +1 month -1 day");
		$this->db->select("sum(money_num) as zhichu");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',2);
		$this->db->where('money_time>',$bg_time);
		$this->db->where('money_time<',$end_time);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
	}
	public function user_chongzhi_mouth($user_id,$month)
	{
		$bg_time = strtotime($month);
		$bg_time_link = date('Y-m-01', strtotime(date("Y-m-d")));
		$end_time = strtotime("$bg_time_link +1 month -1 day");
		$this->db->select("sum(money_num) as chongzhi");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',3);
		$this->db->where('money_time>',$bg_time);
		$this->db->where('money_time<',$end_time);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
	}
//充值
	function getlastmonth($user_id,$date){
    $timestamp=strtotime($date);
     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    $this->db->select("sum(money_num) as chongzhi");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',3);
		$this->db->where('money_time>',$firstday);
		$this->db->where('money_time<',$lastday);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    // return array($firstday,$lastday);
}
//支出
	function getlastmonth_zhichu($user_id,$date){
    $timestamp=strtotime($date);
     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    $this->db->select("sum(money_num) as chongzhi");//sum(money_num)
		$this->db->from('moneys');
		$this->db->where('user_id',$user_id);
		$this->db->where('money_type',2);
		$this->db->where('money_time>',$firstday);
		$this->db->where('money_time<',$lastday);
		$query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    // return array($firstday,$lastday);
}
	// public function getlastmonth($user_id,$date){
 //     $timestamp=strtotime($date);
 //     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
 //     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
 //     // return array($firstday,$lastday);
 //     $this->db->select("sum(money_num) as chongzhi");//sum(money_num)
	// 	$this->db->from('moneys');
	// 	$this->db->where('user_id',$user_id);
	// 	$this->db->where('money_type',3);
	// 	$this->db->where('money_time>',$firstday);
	// 	$this->db->where('money_time<',$lastday);
	// 	$query = $this->db->get();
 //        if($query->num_rows() > 0){
 //            return $query->row_array();
 //        }
 // }
}
